package com.sesame.controller;

import com.sesame.api.FMSConfig;
import com.sesame.entity.OpReq;
import com.sesame.service.NodeService;
import kim.sesame.common.result.ApiResult;
import kim.sesame.common.utils.StringUtil;
import kim.sesame.common.web.controller.AbstractWebController;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/api/node")
public class NodeController extends AbstractWebController {

    @Autowired
    private FMSConfig fmsConfig;

    @Autowired
    private NodeService nodeService;

    @PostMapping("/tree")
    public ApiResult tree() {
        List<Map> list = nodeService.queryNodes(fmsConfig.getFilePath());
        return success(list);
    }

    @PostMapping("/getfilecontent")
    public ApiResult getfilecontent(@RequestBody OpReq req) {
        String path = req.getPath();
        List<String> list;
        try {
            path = StringUtil.removeQuotes(path);
            list = org.apache.commons.io.FileUtils.readLines(new File(path), "UTF-8");
        } catch (IOException e) {
            list = new ArrayList<String>();
            e.printStackTrace();
        }

        return success(list);
    }
}
